package logic

import (
	"context"
	"errors"
	"local-life/liferpc/internal/svc"
	"local-life/liferpc/liferpc"
	"local-life/model"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderAddLogic {
	return &OrderAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *OrderAddLogic) OrderAdd(in *liferpc.OrderAddRequest) (*liferpc.OrderAddResponse, error) {
	// todo: add your logic here and delete this line
	var good model.Good
	total := 0.0
	if in.Quantity > 10 {
		return nil, errors.New("购买数量不能超过10")
	}
	subTotal := float64(in.Quantity) * good.Price
	total += subTotal
	goods := model.Good{
		Name:  in.Name,
		Price: float64(in.Price),
		Stock: 1,
		Total: total,
	}
	err := goods.GoodCreate(model.DB)
	if err != nil {
		return nil, errors.New("订单添加失败,请取消")
	}
	return &liferpc.OrderAddResponse{
		OrderSn: "1",
		Total:   0,
		PayType: "",
	}, nil
}
